import { Module } from 'vuex'
import { getUserInfo } from '/@/api/login'
import Base64 from '/@/utils/base64'
import { store } from '/@/store/index.ts'
import { generateRoutes, dynamicImport } from '/@/router/helps/dynamicImport'
import { viewRouters, dynamicRoutes } from '/@/router/index.ts'
import router from '/@/router/index.ts'
import { RouteRecordRaw } from 'vue-router'

const routesListModule: Module<RoutesListState, RootStateTypes> = {
	namespaced: true,
	state: {
		routesList: [],
		allRoutesList: []
	},
	mutations: {
		// 设置路由，菜单中使用到
		getRoutesList(state: any, data: Array<object>) {
			state.routesList = data
		},
		// 设置路由，菜单中使用到
		getAllRoutesList(state: any, data: Array<object>) {
			state.allRoutesList = data
		}
	},
	actions: {
		// 设置路由，菜单中使用到
		async setRoutesListInit({ commit }, data: any) {
			commit('getRoutesList', data)
		},
		// 设置路由，菜单中使用到
		setRoutesList({ commit }) {
			return new Promise<void>(async resolve => {
				// 获得基础服务地址
				await store.dispatch('baseServices/setBaseData')

				store.dispatch('userInfos/setUserInfos')
				const auth = store.state.userInfos.userInfos

				const authInfo = {
					account: auth.account,
					password: Base64(auth.password, 2)
				}

				const { datas } = await getUserInfo(authInfo)

				if (!datas) {
					throw Error('Verification failed, please Login again.')
				}

				// const { list = []} = datas.menu
				const list = [
					{
						id: 1,
						pId: 0,
						url: 'home',
						icon: 'iconfont icon-shouye',
						name: '首页',
						list: []
					},
					{
						id: 1,
						pId: 0,
						url: 'member',
						icon: 'iconfont icon-icon-',
						name: '会员管理',
						list: []
					},
					{
						id: 1,
						pId: 0,
						url: 'good',
						icon: 'el-icon-sell',
						name: '商品管理',
						list: [
							{
								id: 2,
								pId: 1,
								url: 'goodType',
								icon: 'iconfont icon-ico',
								name: '商品分类',
								list: []
							},
							{
								id: 1,
								pId: 1,
								url: 'goodList',
								icon: 'iconfont icon-caidan',
								name: '商品列表',
								list: []
							}
						]
					},
					{
						id: 1,
						pId: 0,
						url: 'group',
						icon: 'iconfont icon-zujian',
						name: '拼团管理',
						list: [
							{
								id: 2,
								pId: 1,
								url: 'groupRule',
								icon: 'iconfont icon-fuzhiyemian',
								name: '拼团规则',
								list: []
							},
							{
								id: 1,
								pId: 1,
								url: 'groupList',
								icon: 'iconfont icon-shenqingkaiban',
								name: '拼团列表',
								list: []
							}
						]
					},
					{
						id: 1,
						pId: 0,
						url: 'system',
						icon: 'iconfont icon-xitongshezhi',
						name: '系统设置',
						list: [
							{
								id: 1,
								pId: 1,
								url: 'staff',
								icon: 'iconfont icon-caidan',
								name: '员工管理',
								list: []
							},
							{
								id: 2,
								pId: 1,
								url: 'rolePermissions',
								icon: 'iconfont icon-caidan',
								name: '角色权限',
								list: []
							},
							{
								id: 3,
								pId: 1,
								url: 'menu',
								icon: 'iconfont icon-caidan',
								name: '菜单管理',
								list: []
							}
						]
					}
				]
				//格式化路由格式
				const asyncRoutes = generateRoutes(list)

				const dynamicRoutes = [...asyncRoutes, ...viewRouters]

				const app = [
					{
						path: '/',
						name: 'layout',
						component: dynamicImport('/layout/index.vue'),
						redirect: asyncRoutes[0].path, //可能没有home页面，所以取第一个
						meta: {
							isKeepAlive: true
						},
						children: dynamicRoutes
					}
				]

				commit('getRoutesList', dynamicRoutes)
				commit('getAllRoutesList', app)

				resolve()
			})
		}
	}
}

export default routesListModule
